trait Hello {
    fn hello(&self);
}


trait Provider {
    fn sect(&self, name: &str) -> Option<(u32, u32)>;
}


struct Person {
    i: Box<dyn Hello>,
}

struct Map;

impl Provider for Map {
    fn sect(&self, name: &str) -> Option<(u32, u32)> {
        match name {
            "mmio" => Some((0, 0)),
            "redist" => Some((10, 10)),
            &_ => None
        }
    }
}

fn test_map() 
{
    let m = Map{};
    let ret = m.sect("mmio");
    println!("sect is {:?}", ret);
}

impl Person {
    fn hello(&self) {
        self.i.hello()
    }
}

struct Student {
}

impl Hello for Student {
    fn hello(&self) {
        println!("Hello from student");
    }
}



#[derive(Debug)]
struct Teacher {
}
impl Hello for Teacher {
    fn hello(&self) {
        println!("Hello from teacher");
    }
}

fn transfer(h: & impl Hello, h2: &Teacher) {
    h.hello();

    // 不通过，可以将类型转成trait，反过来不行。
    // let h0 = h as &Teacher;
    let h0 = h2 as & dyn Hello;
    h0.hello();

    println!("hello: {:?}", h2);
}

fn main() {
    println!("Hello, world!");
    let s = Student{};
    let p = Person{i: Box::new(s)};
    let t = Teacher{};
    let p2 = Person{i: Box::new(t)};
    p.hello();
    p2.hello();
    let t2 = Teacher{};
    transfer(&t2, &t2);
    test_map();
}
